package com.trade.dao.impl;

import com.trade.dao.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.*;
import java.sql.*;
import java.util.*;
import java.text.*;
import com.trade.data.*;
import com.trade.data.type.*;

public class TestSpring{

	public static void main(String[] args)throws Exception {


		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContextTest.xml");

		BasicDataSource ds = (BasicDataSource)context.getBean("dataSource");

		INSEDataDAO nse = (INSEDataDAO)context.getBean("nseDataDAO");
		final IEquityDAO equity = (IEquityDAO)context.getBean("equityDAO");

		final ILivePriceDAO livePriceDAO = (ILivePriceDAO)context.getBean("livePriceDAO");

		final List<String> tickerList = new ArrayList<String>();
		//tickerList.add("NIFTY");
		tickerList.add("SBIN");
		//tickerList.add("BHEL");

		//Retrieve Previous day data
		HistoricalDataFilterVO historicalDataFilterVO = new HistoricalDataFilterVO();
		historicalDataFilterVO.setTickerList(tickerList);
		historicalDataFilterVO.setInterval(IntervalTimeTypes.T2_MIN);
		historicalDataFilterVO.setNoOfPeriod(1); // 3 days
		List<HistoricalDataVO> histList1 = livePriceDAO.getClosePrices(historicalDataFilterVO );


		new Thread(new Runnable() {

			@Override
			public void run() {
				System.out.println("\n\n\nLive Price Thread started\n\n\n");
				while(true){
					try{
						HistoricalDataFilterVO historicalDataFilterVO = new HistoricalDataFilterVO();
						historicalDataFilterVO.setTickerList(tickerList);
						historicalDataFilterVO.setInterval(IntervalTimeTypes.T2_MIN);
						historicalDataFilterVO.setNoOfPeriod(1);
						List<HistoricalDataVO> histList = livePriceDAO.getIntradayClosePrices(historicalDataFilterVO );
						System.out.println();
						Thread.sleep(120000);
					}catch(Exception e){
						System.out.println(e.getLocalizedMessage());
					}
				}
			}
		}).start();


		/*List<OptionDataVO> testList = nse.getAllOptionScrips( new InstrumentVO());


		List<InstrumentVO> instrumentVOList = nse.getAllStockScripts();
		Long i = 0L ;
		for(InstrumentVO instrumentVO : instrumentVOList){
			instrumentVO.setId(i++);
			equity.addEquity(instrumentVO);
		}*/

		/*DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");


		com.trade.data.HistoricalDataFilterVO test = new com.trade.data.HistoricalDataFilterVO();
		test.setSymbol("SBIN");
		test.setSeries("EQ");
		test.setSegment(Segment.EQUITY.getId()); //
		test.setFromDate( formatter.parse("20-10-2009"));
		test.setToDate( formatter.parse("20-10-2012") );

		List<HistoricalDataVO> stockHistDataList =  nse.getHistoricalData( test);


		com.trade.data.HistoricalDataFilterVO indexData = new com.trade.data.HistoricalDataFilterVO();
		indexData.setSymbol("S&P CNX NIFTY");
		indexData.setSeries("EQ");
		indexData.setSegment(Segment.INDEX.getId());
		indexData.setFromDate( formatter.parse("01-10-2012"));
		indexData.setToDate( formatter.parse("20-10-2012") );


		List<HistoricalDataVO> indexHistDataList =  nse.getHistoricalData( indexData); */


		/*Long i = 0L ;
		for(EquityHistoricalDataVO stockHistData : stockHistDataList){
			stockHistData.setId(i++);
			equity.addHistoricalData(stockHistData);
		}*/

		/*System.out.println("Conn - "+ds.getConnection());

		//Equity data
		com.trade.data.InstrumentVO test = new com.trade.data.InstrumentVO();
		test.setId(123L);
		test.setSymbol("SBIN");

		EquityHistoricalDataVO ehdv = new EquityHistoricalDataVO();
		ehdv.setId(123L);
		ehdv.setSymbol("SBIN");

		EquityIntradayDataVO eidv = new EquityIntradayDataVO();
		eidv.setId(123L);
		eidv.setSymbol("SBIN");

		//Indices data

		com.trade.data.IndexVO index = new com.trade.data.IndexVO();
		index.setId(123L);
		index.setSymbol("NIFTY");

		IndexHistoricalDataVO indexhist = new IndexHistoricalDataVO();
		indexhist.setId(123L);
		indexhist.setSymbol("NIFTY");

		IndexIntradayDataVO indexintra = new IndexIntradayDataVO();
		indexintra.setId(123L);
		indexintra.setSymbol("NIFTY");

		//Option data

		com.trade.data.OptionDataVO option = new com.trade.data.OptionDataVO();
		option.setId(123L);
		option.setSymbol("NIFTY");
		option.setOptionType(OptionTypes.CE);
		option.setInstrumentType(InstrumentTypes.OPTIDX);

		OptionActiveScriptVO oas = new OptionActiveScriptVO();
		oas.setId(123L);
		oas.setSymbol("NIFTY");
		oas.setLastTradePrice(35.0);


		OptionIntradayScriptVO oid = new OptionIntradayScriptVO();
		oid.setId(123L);
		oid.setSymbol("NIFTY");
		oid.setLastTradePrice(35.0);

		OptionIntradayTopFiveOrderVO oitf = new OptionIntradayTopFiveOrderVO();
		oitf.setId(123L);
		oitf.setSymbol("NIFTY");

		//Order data


		SessionFactory sf = (SessionFactory)context.getBean("sessionFactory");

		Session session = sf.getCurrentSession();
		Transaction tx = session.beginTransaction();

		session.save(test);
		session.save(ehdv);
		session.save(eidv);


		session.save(index);
		session.save(indexhist);
		session.save(indexintra);


		session.save(option);
		session.save(oas);
		session.save(oid);
		session.save(oitf);




		tx.commit();*/

	}


}